//app.js
App({
  ReqUrl: 'https://www.leejaz.com/school/school_card/index.php/Home/',
  Cookie: Math.random().toString(36).substr(2),//生成随机数字
  onLaunch: function () {
    // 展示本地存储能力
    var t = this;
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        wx.request({
          url: t.ReqUrl + 'wxlogin/wxlogin',
          data: {
            code: code,
            appid: 'wx4407bf503c24bbee',
            appsecret: '264b73f740e5f2a647cfe714589ee30a'
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Cookie': 'PHPSESSID=' + t.Cookie + ';path=/'
          },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: res => {
            console.log(res);
            var openid_key = res.data.data.openid_key;
            t.openid_key = openid_key;
            console.log(t.openid_key);
<<<<<<< HEAD
            // 接口5
            t.postData('user/search', {}, res => {
              // t.setData({
              //   imgs: data.top_img,
              // })
              console.log('同步图片');
              console.log(res);
            })
=======
>>>>>>> 463878e3c7d846902946aa3388e816892fa16653
          },
        })
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  postData2(url, data, callback) {
    var ip = this.ReqUrl;
    wx.request({
      url: ip + url,
      data: {
        data: JSON.stringify(data),
        openid: 'oIxOZ5UvNRnUCxIsJ2EUVg6LVfDA',
        openid_key: this.openid_key
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + this.Cookie + ';path=/'
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      complete: res => {
        callback(res)
      }
    });
  },
  //post请求（封装）
  postData(url, data, callback) {
    var ip = this.ReqUrl;
    wx.request({
      url: ip + url,
      data: {
        data: JSON.stringify(data),
        openid_key: this.openid_key
        }, 
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + this.Cookie + ';path=/'
         },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      complete: res => {
        callback(res)
      }
    });
  },
  globalData: {
    userInfo: null,
    imgIP: 'https://www.leejaz.com/school/school_card/'
  }
})